package modalities;



import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;


import com.phidgets.InterfaceKitPhidget;


public class Keyboard extends Observable implements KeyListener {

	double minPos = 0;
	double maxPos = 1000;
	double incrementPos = 30.0;
	
	double minAngle = 0.0;
	double maxAngle = 360.0;
	double incrementAngle = 5.0;

	static double angle;
	static double position;

	//ModifSimulation th;
	Decider boss;

	static InterfaceKitPhidget interfaceKitPhidget;
		
	
	public Keyboard(Decider boss)
	{
		this.boss = boss;
		//The decider watch at the phidget
		addObserver(boss);

	}

	// Rotation detected
	public void newAngle(Double value){
		setChanged();
		notifyObservers(new Message(value));
	}

	// Vertical position changed
	public void newPosition(Integer position){
		setChanged();
		notifyObservers(new Message(position));
	}
	
	// Vertical position changed
	public void tapDetected(Boolean detected){
		setChanged();
		notifyObservers(new Message(detected,0.6));
	}
	
	// Vertical position changed
	public void skipScreen(char chararcter){
		setChanged();
		notifyObservers(new Message(chararcter));
	}
	

	@Override // Simulate the phidget turn button
	public void keyPressed(KeyEvent e) {
		
		// Turn phidget up
		if (e.getKeyCode() == KeyEvent.VK_LEFT )
		{
			angle = angle-incrementAngle;
			if(angle <= minAngle)
				angle = maxAngle;
			newAngle(angle);
			
		}
		//Turn phidget down
		if (e.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			angle = angle + incrementAngle;
			if(angle >= maxAngle)
				angle = minAngle;
			newAngle(angle);
		}
		//Slider up
		if (e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			position = position + incrementPos;
			if(position >= maxPos)
				position = maxPos;
			newPosition((int) position);
		}
		//Slider up
		if (e.getKeyCode() == KeyEvent.VK_UP )
		{
			position = position - incrementPos;
			if(position <= minPos)
				position = minPos;
			newPosition((int) position);
		}
		//Tap on the button
		if (e.getKeyCode() == KeyEvent.VK_ENTER )
		{
			tapDetected(true);
		}
		//Interact with user (pause, skip welcome screen...
		if (e.getKeyCode() == KeyEvent.VK_N )
		{
			skipScreen('n');
		}
		//Interact with user (pause, skip welcome screen...
		if (e.getKeyCode() == KeyEvent.VK_Q )
		{
			skipScreen('q');
		}
		//Interact with user (pause, skip welcome screen...
		if (e.getKeyCode() == KeyEvent.VK_R )
		{
			skipScreen('r');
		}
		//Interact with user (pause, skip welcome screen...
		if (e.getKeyCode() == KeyEvent.VK_SPACE )
		{
			skipScreen(' ');
		}
		//Interact with user (pause, skip welcome screen...
		if (e.getKeyCode() == KeyEvent.VK_H )
		{
			Message fusionVoicePressure = new Message(true, "hit");
			setChanged();
			notifyObservers(fusionVoicePressure);
		}
	
		
	}

	//Unimplemented listner
	@Override
	public void keyReleased(KeyEvent arg0){}
	@Override
	public void keyTyped(KeyEvent arg0) {}

}
